package table.models;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import jp.gr.java_conf.tame.swing.table.AttributiveCellTableModel;
import jp.gr.java_conf.tame.swing.table.CellSpan;
import jp.gr.java_conf.tame.swing.table.MultiSpanCellTable;

public class RankingTable extends javax.swing.JTable {
	private String title;

	public void setTitle(String title) {
		this.title = title;
	}

	public String getTitle() {
		return this.title;
	}

	public JTable toJTable() {
		int numColumns = this.getColumnCount();
		int numRows = this.getRowCount();
		JTable toReturn = new JTable();
		DefaultTableModel model = new DefaultTableModel(numRows, numColumns);
		for (int row = 0; row < numRows; row++) {
			for (int column = 0; column < numColumns; column++) {
				model.setValueAt(this.getValueAt(row, column), row, column);
			}
		}
		toReturn.setModel(model);
		return toReturn;
	}

	public MultiSpanCellTable toCellSpanTable() {
		int numColumns = this.getColumnCount();
		int numRows = this.getRowCount();
		AttributiveCellTableModel model = new AttributiveCellTableModel(numRows, numColumns);
		MultiSpanCellTable toReturn = new MultiSpanCellTable(model);

		for (int row = 0; row < numRows; row++) {
			for (int column = 0; column < numColumns; column++) {
				model.setValueAt(this.getValueAt(row, column), row, column);
			}
		}

		CellSpan cellAtt = (CellSpan) model.getCellAttribute();
		int [] totalWidth = new int [numColumns];
		for (int i = 0; i < totalWidth.length; i++) {
			totalWidth [i] = i;
		}
		for (int row = 0; row < numRows; row++) {
			if ((model.getValueAt(row, 0) == null) || model.getValueAt(row, 0).equals("")) {
				cellAtt.combine(new int [] { row }, totalWidth);
			}
			if (model.getValueAt(row, 0).equals("Rang")) {
				cellAtt.combine(new int [] { row - 2 }, totalWidth);
				cellAtt.combine(new int [] { row - 2, row - 1 }, totalWidth);
			}
		}

		// toReturn.setModel(model);
		return toReturn;
	}
}
